﻿using System;
using System.Collections.Generic;
using Core.Interfaces;
using System.ComponentModel.Composition;
using System.Web;
using Core.Interfaces;
using Core.DataAccess;

namespace Core.Components
{
    [Export(typeof(IWebContext))]
    public class WebContext : Core.Interfaces.IWebContext
    {

        public void ClearSession()
        {
            HttpContext.Current.Session.Clear();
        }

        public bool ContainsInSession(string key)
        {
            return HttpContext.Current.Session[key] != null;
        }

        public void RemoveFromSesson(string key)
        {
            HttpContext.Current.Session.Remove(key);
        }


        private object GetFromSession(string key)
        {
            if (HttpContext.Current == null || HttpContext.Current.Session == null)
            {
                return null;
            }
            else
                return HttpContext.Current.Session[key];
        }

        private void SetInSession(string key, object value)
        {
            if (HttpContext.Current == null || HttpContext.Current.Session == null)
            {
                return;
            }
            HttpContext.Current.Session[key] = value;
        }

        public bool IsLogged
        {
            get 
            {
                if (ContainsInSession("IsLogged"))
                {
                    if ((bool)GetFromSession("IsLogged"))
                        return true;
                    else
                        return false;
                }
                else
                    return false;
            }
            set 
            {
                SetInSession("IsLogged", value);
            }
        }

        public Account CurrentUser
        {
            get
            {
                if (ContainsInSession("CurrentUser"))
                {
                    return GetFromSession("CurrentUser") as Account;
                }
                return null;
            }
            set 
            {
                SetInSession("CurrentUser", value);
            }
        }

        public string UserName
        {
            get
            {
                if (ContainsInSession("UserName"))
                {
                    return GetFromSession("UserName").ToString();
                }
                return "";
            }
            set
            {
                SetInSession("UserName", value);
            }
        }
        
        public int AccountID
        {
            get 
            {
                if (!string.IsNullOrEmpty(GetQueryStringValue("AccountID")))
                {
                    return Convert.ToInt32(GetQueryStringValue("AccountID"));
                }
                return 0;
            }
            set
            {
                SetInSession("AccountID", value);
            }
        }

        public bool ShowGravatar
        {
            get 
            {
                if (!string.IsNullOrEmpty(GetQueryStringValue("ShowGravatar")) && GetQueryStringValue("ShowGravatar") == "1")
                {
                    return true;
                }
                return false;
            }
        }

        public string UsernameToVerify
        {
            get
            {
                return GetQueryStringValue("a").ToString();
            }
        }

        private string GetQueryStringValue(string key)
        {
            return HttpContext.Current.Request.QueryString.Get(key);
        }

        public string RootUrl
        {
            get
            {
                string result;

                string Port = HttpContext.Current.Request.ServerVariables["SERVER_PORT"];
                if (Port == null || Port == "80" || Port == "443")
                    Port = "";
                else
                    Port = ":" + Port;

                string Protocol = HttpContext.Current.Request.ServerVariables["SERVER_PORT_SECURE"];
                if (Protocol == null || Protocol == "0")
                    Protocol = "http://";
                else
                    Protocol = "https://";

                result = Protocol + HttpContext.Current.Request.ServerVariables["SERVER_NAME"] +
                    Port + HttpContext.Current.Request.ApplicationPath;

                return result;
            }
        }


        public long AlbumID
        {
            get 
            {
                Int64 result;
                if (!string.IsNullOrEmpty(GetQueryStringValue("AlbumID")))
                    result = Convert.ToInt64(GetQueryStringValue("AlbumID"));
                else
                    result = 0;
                return result;
            }
        }


        public int FileTypeID
        {
            get 
            {
                Int32 result;
                if (!string.IsNullOrEmpty(GetQueryStringValue("FileTypeID")))
                    result = Convert.ToInt32(GetQueryStringValue("FileTypeID"));
                else
                    result = 0;
                return result;
            }
            set
            {
                SetInSession("FileTypeID",value);
            }
        }


        public string FilePath
        {
            get { return HttpContext.Current.Request.ServerVariables["APPL_PHYSICAL_PATH"].ToString(); }
        }


        public HttpFileCollection Files
        {
            get
            {
                if (HttpContext.Current.Request.Files != null)
                    return HttpContext.Current.Request.Files;
                else
                    return null;
            }
        }

        public int OtherAccountID
        {
            get
            {
                if (!string.IsNullOrEmpty(GetQueryStringValue("otherAccountID")))
                {
                    return Convert.ToInt32(GetQueryStringValue("otherAccountID"));
                }
                return 0;
            }
            set 
            {
                SetInSession("otherAccountID", value);
            }
        }
    }
}
